<?php



	include_once('Commons.php');

	

	$action = param('ac');

	$currentUser = getUser( param('u') );

	$latestProfile = getLatestProfile( $currentUser['id'] );

	$ajax_action = param('ajac');
	
	if ($ajax_action) {
	
	$action = $ajax_action;
	
	}
		

	if ( $action=='validateprofile' )

	{

		$description = param('description');

		$complements = param('complements');
		

		query("UPDATE profile SET current=0 WHERE user='${currentUser[id]}';");

		query("UPDATE profile SET valid=1 , description='$description' , current=1 WHERE id='${latestProfile[id]}';");

		

		$username = $currentUser['username'];

		$parameters = array($username , "http://www.penpalhub.com/profile/$username", "http://www.penpalhub.com/account/profile/", "http://www.globalpenfriends.com/", "http://www.globalpenfriends.com/index.php?page=penpal_competition" );

		

		sendMail('validate', $currentUser['email'], $currentUser['username'], $parameters);

		

		processUserMessages($currentUser['id']);

		

		//logEvent(4, '', $currentUser['id']);

		$ajaxmode = param('ajaxmode');
		
		if ($ajaxmode=="yes") {
		
			echo "success";
			
			die();
			
			return;
			
		}
		
		redirect('Adm.php');
		

	}

	else if ( $action=='refuseprofile' )

	{

		$complements = param('complements');

		$username = $currentUser['username'];

		

		// decline by stuart start
		

		query("UPDATE profile SET current=0 WHERE user='${currentUser[id]}';");

		query("UPDATE profile SET status=1 WHERE user='${currentUser[id]}';");

		query("UPDATE profile SET valid = NULL WHERE id='${latestProfile[id]}';");

		



		// decline by stuart end

				
		$ajaxmode = param('ajaxmode');
		
		if ($ajaxmode=="yes") {
		
			echo "success";
			
			die();
			
			return;
			
		} 

		redirect('Adm.php');

	}

	

	if ( $action=='setcontinent' )

	{

		

		query	("update profile set continent = 10

		where country in ('4', '8', '13', '51', '72', '76','112', '134', '164', '166', '194', '213', '219', '228', '234') ");

		

		query	("update profile set continent = 9

		where country in ('223', '85', '99', '104', '202') ");





		query	("update profile set continent = 8

		where country in ('1', '18', '25', '32', '36', '44', '88', '90', '97', '100', '101', '105', '108', '110', '113', '114', '116', '117', '130', '143', '150', '169', '191', '199', '207', '208', '210', '217', '227', '231') ");

		

		query	("update profile set continent = 7

		where country in ('17', '56', '102', '103', '109', '115', '117', '162', '163', '174', '187', '206', '216', '222', '235') ");

		

		query 	("update profile set continent = 6

		where country in ('3', '6', '23', '28', '34', '35', '37', '39', '41', '42', '48', '50', '54', '59', '64', '66', '67', '69', '78', '79', '82', '91', '92', '111', '121', '128', '129', '132', '136', '137', '138', '145', '146', '148', '156', '157', '178', '179', '188', '189', '190', '195', '196', '200', '203', '209', '211', '215', '220', '237', '238', '131', '122') ");

		

		query 	("update profile set continent = 5

		where country in ('2', '11', '15', '20', '27', '33', '55', '57', '68', '80', '84', '98', '118','124', '127', '141', '171', '176', '177', '192', '193', '221', '236') ");

		

		query 	("update profile set continent = 4

		where country in ('5', '14', ' 21', '58', '73', '74', '81', '83', '106', '123', '125', '126', '133', '142', '152', '161', '172', '185', '198', '204', '205', '229', '215') ");

		

		query 	("update profile set continent = 3

		where country in ('10', '22', '26', '30', '43', '47', '52', '63', '65', '70', '75', '93', '96', '155', '165', '167', '168', '201', '226', '230') ");

		

		query 	("update profile set continent = 2

		where country in ('7', '9', '12', '16', '19', '24', '31', '40', '60', '61', '86', '87', '89', '94', '107', '135', '144', '151', '173', '180', '181', '183', '214', '218', '233') ");

		

		query 	("update profile set continent = 1

		where country in ('224', '38', '139', '182', '225') ");



		
		$ajaxmode = param('ajaxmode');
		
		if ($ajaxmode=="yes") {
		
			echo "success";
			
			die();
			
			return;
			
		}

		redirect('Adm.php');

	}





	else if ( $action=='deleteprofile' )

	{

		$complements = param('complements');

		$username = $currentUser['username'];

		

		// delete by stuart start

		query("DELETE from message WHERE `from`='${currentUser[id]}';");

		query("DELETE from profile WHERE user='${currentUser[id]}';");

		query("DELETE from user WHERE id='${currentUser[id]}';");

		



		// delete by stuart end
		$ajaxmode = param('ajaxmode');
		
		if ($ajaxmode=="yes") {
		
			echo "success";
			
			die();
			
			return;
			
		}

		redirect('Adm.php');

	}



	else

	{

		$rh = query('	SELECT u.username

						FROM user u

						LEFT JOIN profile p2 ON p2.user=u.id AND p2.current=1

						LEFT JOIN profile p1 ON p1.user=u.id

						WHERE p1.creation>p2.creation AND p1.status IS NULL

						OR p2.creation IS NULL AND p1.status IS NULL

						GROUP BY u.id

						ORDER BY p1.id DESC;');

		

		$users = array();

		

		while ( $row = mysql_fetch_assoc($rh) )

			array_push($users, $row);

	}

	

	

	if ( $action=='validate' || $action=='ajax' )

	{

		pushNavigationItem('Admin', '/Scripts/Adm.php');

		pushNavigationItem('Validate profile', '/Scripts/Adm.php');

		if ($action!='ajax') {
		printHeader('Validate profile');
		}

		

		

		

		

		print '<form class="frmDetails" id="frm_' . $currentUser[username] . '" action="/Scripts/Adm.php" method="post">';

		

		print "<input type='hidden' name='u' value='${currentUser[username]}' />";
		
		print "<input type='hidden' name='ajac' id='ajac_${currentUser[username]}' value='' />";
		
		if ($action=='ajax') {
		
		print "<input type='hidden' name='ajaxmode' value='yes' />";
		
		}

		// print "<input type='hidden' name='ac' value='validatesubmit' />";

		

		print "<label for='description'>Description</label><br/>";

		print "<textarea name='description' id='description'>${latestProfile[description]}</textarea>";

		

		print '<br/>';

		print '<br/>';

		

		print 'Region : '. $latestProfile['continent'];

		print '<br/>';

		print '<br/>';

		

		print '<input type="submit" name="ac" value="validateprofile" onclick="return submit_click(' . "'${currentUser[username]}','validateprofile');\" />";

		print '<input type="submit" name="ac" value="refuseprofile" onclick="return submit_click(' . "'${currentUser[username]}','refuseprofile');\" />";

		print '<input type="submit" name="ac" value="deleteprofile" onclick="return submit_click(' . "'${currentUser[username]}','deleteprofile');\" />";

		print '<input type="submit" name="ac" value="setcontinent" onclick="return submit_click(' . "'${currentUser[username]}','setcontinent');\" />";





		print '<br/>';
		
		print "<div id='processing_${currentUser[username]}'></div>";

		print '<br/>';

		

		print "<label for='complements'>Comments</label><br/>";

		print "<textarea name='complements' id='complements'></textarea>";

		

		print '<br/>';

		print 'email : '. $currentUser['email'];

		print '<br/>';

		

		

		



		print '</form>';

		

		if ( ! empty($latestProfile['type']) )

			print "<p><img src='/Thumbs/${latestProfile[photo]}-medium.${latestProfile[type]}' /></p>";

		

		$rh = query("SELECT m.* FROM message m WHERE m.from=${currentUser[id]};");

		

		while ( $message = mysql_fetch_assoc($rh) )

		

			print "<p>${message[message]}</p>";

		
		if ($action!='ajax') {
		printFooter();
		}

	}
	

	else

	{

		pushNavigationItem('Admin', '/Scripts/Adm.php');

		pushNavigationItem('Recent profiles', 'Adm.php');

		printHeader('Recent profiles');

		
		print '<style>';

		print '.uinfo {clear: both; width: 720px; padding-left: 20px; margin-bottom: 20px;}' . "\r\n";

		print '.uname {float:left; width: 320px; }' . "\r\n";

		print '.uview {float:left; width: 150px; }' . "\r\n";

		print '.uscore {float:left; width: 150px; }' . "\r\n";
		
		print '.buttons {float:left; width: 100px; }' . "\r\n";

		print '.clr {clear:both;}' . "\r\n";

		print '.quickview {width: 720px; background-color: #eeeeee;}' . "\r\n";

		print '</style>';
		
?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<script type="text/javascript">

function load_detail(uname) {

if ($('#quickview_' + uname).html()) {
	if ($('#quickview_' + uname).css('display') == 'none') 
	
	{
	
		$('#quickview_' + uname).show();
	
	}
	
	else 
	
	{
	
		$('#quickview_' + uname).hide();
		
	}
	
}

else 

{

var request;

$('#link_quickview_' + uname).html('loading...'); 

$.get('/Scripts/Adm.php?ac=ajax&u=' + uname, function(data) {

  $('#quickview_' + uname).html(data);
  
  $('#link_quickview_' + uname).html('<a href="javascript:load_detail(' + "'" + uname + "'" + ')">Quick View</a>');
  
  
	// bind to the submit event of our form 

	$("#frm_" + uname).submit(function(event){

		// abort any pending request

		if (request) {
		
			request.abort();
			
		}
		
		// setup some local variables
		
		var $form = $(this);
		
		// let's select and cache all the fields
		
		var $inputs = $form.find("input, select, button, textarea");
		
		// serialize the data in the form
		
		var serializedData = $form.serialize();

		// let's disable the inputs for the duration of the ajax request
		
		$inputs.prop("disabled", true);

		$('#processing_' + uname).html("processing...");

		// fire off the request to /form.php
		
		request = $.ajax({
		
			url: "/Scripts/Adm.php",
			
			type: "post",
			
			data: serializedData
			
		});

		// callback handler that will be called on success
		
		request.done(function (response, textStatus, jqXHR){
		
			// log a message to the console
			
			console.log(response);
			
			$('#processing_' + uname).html("<b>DONE!</b>");
			
			$('#uinfo_' + uname).hide(2000);
			
			//alert($form.id);
			
		});

		// callback handler that will be called on failure
		
		request.fail(function (jqXHR, textStatus, errorThrown){
		
			// log the error to the console
			
			console.error("The following error occured: " + textStatus, errorThrown);
			
		});

		// callback handler that will be called regardless
		
		// if the request failed or succeeded
		
		request.always(function () {
		
			// reenable the inputs
			
			$inputs.prop("disabled", false);
			
		});

		// prevent default posting of form
		
		event.preventDefault();
		
	});  

});

}

}

function submit_click(uname, acvalue) {
	$("#ajac_" + uname).val(acvalue);
	return true;
}
</script>

<?php		

		print "<div class='uinfo'><div class='uname'><b>USERNAME</b></div><div class='uview'><b>QUICK VIEW</b></div><div class='uscore'><b>SCORE</b></div><div class='buttons'><b>SELECT</b></div><div class='clr'></div></div>";

		foreach ( $users as $u )

		{
		
			$uscore = "&nbsp;";

			print "<div class='uinfo' id='uinfo_${u[username]}'><div class='uname'><a href='/Scripts/Adm.php?ac=validate&u=${u[username]}'>${u[username]}</a></div><div class='uview'><a href=\"javascript:load_detail('${u[username]}')\" id='link_quickview_${u[username]}'>Quick View</a></div><div class='uscore'>{$uscore}</div><div class='buttons'><input type='checkbox' value='${u[username]}' id='chk_${u[username]}'/></div><div class='clr'></div><div class='quickview' id='quickview_${u[username]}'></div></div>";

		}

		


?>

<script type="text/javascript">

// variable to hold request 

var request;


$(document).ready(function() {
  

  
});

</script>
<?php		

		printFooter();

	}



?>